/**
 * Created by lins on 2018-7-16.
 */
(function (angular) {
    function LabelRetrievalService($resource, $q, $http, UrlConfigService) {
        var _schema = [
            {label: '序号', name: "no", sortable: false},
            {label: '标签名称', name: "name", sortable: false},
            {label: '企业数', name: "count", sortable: false},
            {label: '操作', type: 'template', templateUrl: 'operation.html', width: '160px'}
        ];

        var _businessSchema = [
            {label: '序号', name: "no", sortable: false},
            {label: '企业名称', name: "name", sortable: false},
            {label: '企业标签', name: "label", sortable: false},
            {label: '操作', type: 'template', templateUrl: 'operation.html', width: '160px'}
        ];
        this._schema = _schema;
        this._businessSchema= _businessSchema;
        this._sort = '';
        this._order = '';
        this._listUrl = UrlConfigService.urlConfig. decisionAnalysis.portray.labelRetrieval.list;
        this._url = UrlConfigService.urlConfig. decisionAnalysis.portray.labelRetrieval.url;
        this._businessUrl = UrlConfigService.urlConfig. decisionAnalysis.portray.labelRetrieval.business;

        BaseListService.call(this, this._listUrl, $resource, $q, $http, _schema, this._url);

        this.getBusinessSchema = function () {
            return this._businessSchema;
        };

        this.getBusiness=function () {
            return this.$resource(this._businessUrl).get();
        }

        this.getBusinessList = function (placeholder, cb) {
            var service = this;
            var url = this._businessUrl;
            if (typeof placeholder === 'function') {
                cb = placeholder;
                placeholder = undefined;
            }
            var queryMap = {page: this.getStoredPage(), size: this._pageable.size};
            if (this._s_search) {
                for (var k in this._s_search) {
                    if (this._s_search.hasOwnProperty(k) && this._s_search[k] !== undefined && this._s_search[k] !== "")
                        queryMap['s_' + k] = this._s_search[k];
                }
            }
            queryMap.sort = service.getSortAndOrder();
            var resource = this.$resource(url, !!placeholder ? placeholder : {});
            if (cb) {
                resource.get(queryMap).$promise.then(function (res) {
                    for(var key in res.pageable)
                    {
                        service._pageable[key] = res.pageable[key];
                    }
                    cb(res);
                });
            }
            else {
                return resource.get(queryMap);
            }
        };

    }

    LabelRetrievalService.$inject = ['$resource', '$q', '$http', 'UrlConfigService'];
    angular.module('MetronicApp').service('LabelRetrievalService', LabelRetrievalService);
})(angular);